﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Security.Permissions;

using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

using Microsoft.SharePoint.Administration;

using JobShopsDiscussion_v01_6_App18;

namespace ImapTest_WinForms
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Imap.KoImap o = new Imap.KoImap();
            MAPIFolder folder = o.getKoInFolder();

            if (folder != null)
            {
                foreach (MailItem item in folder.Items)
                {
                }
            }
            
            // MessageBox.Show(res);
        }

        private void exchWebServiceTestToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void testToolStripMenuItem2_Click(object sender, EventArgs e)
        {

            Ko2.Portal.Env env = new Ko2.Portal.Env();
            Imap.KoExchWebService o = new Imap.KoExchWebService();
            string uid = env.GetAppSetting("FarmUserUID");
            if (String.IsNullOrEmpty(uid))
                return;
            string [] separator = {@"\"};
            string [] uid2 = uid.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            if (uid2.Length < 2)
                return;
            uid = uid2[1];
            string pwd = env.GetAppSetting("FarmUserPwd");
            if (String.IsNullOrEmpty(pwd))
                return;
            string DomainName = env.GetAppSetting("ProxyDomainName");
            if (String.IsNullOrEmpty(DomainName))
                return;
            o.Get_SP_Q_Msg(uid, pwd, DomainName, "SP10_spfarm@korablik.ru");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            testToolStripMenuItem2_Click(null, null);
        }

        private void instToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                //using (SPSite site = properties.Feature.Parent as SPSite)
                //{

                    try
                    {

                        SPWebApplication webApp = SPWebApplication.Lookup(new Uri(@"http://localhost/shops"));
                        //SPServer srv = new SPServer("S-WSS02");

                        // delete preview job item
                        foreach (SPJobDefinition job in webApp.JobDefinitions)
                        {
                            if (
                                    job.Name == "JobShopsDiscussion_v1"
                                || job.Name == "JobShopsDiscussion_v2"
                                || job.Name == "JobShopsDiscussion_v3"
                                || job.Name == "JobShopsDiscussion_v4"
                                || job.Name == "JobShopsDiscussion_v5"
                                || job.Name == "JobShopsDiscussion_v6"
                                || job.Name == "JobShopsDiscussion_v7"
                                )
                            {
                                job.Delete();
                            }
                        }
                        JobShopsDiscussion_JobClass_v01_6 listLoggerJob =
                            new JobShopsDiscussion_JobClass_v01_6("JobShopsDiscussion_v7", webApp);
                        //new JobShopsDiscussion_JobClass_v01_6("JobShopsDiscussion_v7", site.WebApplication);
                        SPDailySchedule schedule = new SPDailySchedule();
                        schedule.BeginHour = 1;
                        schedule.BeginMinute = 0;
                        schedule.BeginSecond = 0;
                        schedule.EndHour = 1;
                        schedule.EndMinute = 59;
                        schedule.EndSecond = 59;

                        listLoggerJob.Schedule = schedule;
                        listLoggerJob.Update();

                    }
                    catch (System.Exception err)
                    {
                    }
                //}

            });
        }

        private void unInstJobShopsDiscussionv7ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                //SPWebApplication webApp = SPWebApplication.Lookup(new Uri(@"http://localhost/shops"));
                //SPServer srv = new SPServer("S-WSS02");

                using (SPSite site = new SPSite(@"http://localhost/shops"))
                {
                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (
                               job.Name == "JobShopsDiscussion_v1"
                            || job.Name == "JobShopsDiscussion_v2"
                            || job.Name == "JobShopsDiscussion_v3"
                            || job.Name == "JobShopsDiscussion_v4"
                            || job.Name == "JobShopsDiscussion_v5"
                            || job.Name == "JobShopsDiscussion_v6"
                            || job.Name == "JobShopsDiscussion_v7"
                            )
                        {
                            job.Delete();
                        }
                    }
                }

            });

        }
    }

}
